package org.edu.dao.impl;



import java.util.List;

import javax.annotation.Resource;

import org.edu.bean.Role;
import org.edu.bean.Role_bar;
import org.edu.bean._a_navigationbar;
import org.edu.bean._b_navigationbar;
import org.edu.bean._navigationbar;
import org.edu.dao.NavigationBarDao;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

@Service
public class NavigationBarDaoImpl implements NavigationBarDao {
	@Resource
	private HibernateTemplate hibernateTemplate;

	@SuppressWarnings("unchecked")
	public _navigationbar selectNavigationbars(Role_bar roleBar) {
		List ls =hibernateTemplate.find("from _navigationbar n where n.id = "+roleBar.getBarid().split("_")[0]) ;
		_navigationbar navigationbar = null  ;
		if(ls.size()>0){
			navigationbar = (_navigationbar) ls.get(0);
		}
		return navigationbar ;
	}

	@SuppressWarnings("unchecked")
	public List<Role_bar> selectRole_barFromRole(Role role , String navigationbar,int length) {
		return hibernateTemplate.find("from Role_bar rb where rb.roleid = "+role.getId()+"and Length(rb.barid) = "+length+" and rb.barid like '"+navigationbar+"_%' order by sort asc");
	}

	@SuppressWarnings("unchecked")
	public _a_navigationbar select_a_navigationbar(Role_bar roleBar) {
		_a_navigationbar aNavigationbar = null ;
		if(roleBar.getBarid().length()==6){
			String _a_barId = roleBar.getBarid().split("_")[1];
			_a_barId = Integer.parseInt(_a_barId)+"";
			List ls =  hibernateTemplate.find("from _a_navigationbar _a where _a.id = "+_a_barId);
			if(ls != null){
				aNavigationbar = (_a_navigationbar) ls.get(0);
			}
		}
		
		return aNavigationbar ;
	}

	@SuppressWarnings("unchecked")
	public _b_navigationbar select_b_navigationbar(Role_bar roleBar) {
		_b_navigationbar bNavigationbar = null ;
		if(roleBar.getBarid().length()==11){
			String _b_barId = roleBar.getBarid().split("_")[2];
			_b_barId = Integer.parseInt(_b_barId)+"";
			List ls = hibernateTemplate.find("from _b_navigationbar _b where _b.id = "+_b_barId);
			if(ls!=null){
				bNavigationbar = (_b_navigationbar) ls.get(0);
			}
		}
		return bNavigationbar ;
	}

	@SuppressWarnings("unchecked")
	public List<_navigationbar> selectAll() {
		return hibernateTemplate.find("from _navigationbar");
	}

	@SuppressWarnings("unchecked")
	public List<_a_navigationbar> selectFromPid_a_navigationbar(
			_navigationbar navigationbar) {
		return hibernateTemplate.find("from _a_navigationbar _a where _a.pid = "+navigationbar.getId());
	}

	@SuppressWarnings("unchecked")
	public List<_b_navigationbar> selectFromPid_b_navigationbar(
			_a_navigationbar aNavigationbar) {
		return hibernateTemplate.find("from _b_navigationbar _b where _b.pid = "+aNavigationbar.getId());
	}

	public Integer createBar(Role_bar roleBar) {
		
		return (Integer)hibernateTemplate.save(roleBar);
	}

	public Integer delBars(Role role) {
		List<Role_bar> list = getAllBarFromRole(role);
		for (Role_bar roleBar : list) {
			hibernateTemplate.delete(roleBar);
		}
		return list.size();
	}

	@SuppressWarnings("unchecked")
	public List<Role_bar> getAllBarFromRole(Role role) {
		return hibernateTemplate.find("from Role_bar rb where rb.roleid = "+role.getId());
	}

	@SuppressWarnings("unchecked")
	public List<Role_bar> selectRoleBarByRoleIdBarVal(String barid, Integer roleid) {
		return (List<Role_bar>)hibernateTemplate.find("from Role_bar rb where rb.roleid = "+roleid+" and rb.barid = '"+barid+"'");
	}

	public boolean delRoleBar(Role_bar roleBar) {
		hibernateTemplate.delete(roleBar);
		return true;
	}


	

	

	
}
